package huoye.shops.huoye.operation.widget;

import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.codeideology.android.widget.ios.dialog.base.BaseDialog;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import huoye.shops.R;


/**
 * Created by uesr on 2018/8/11.
 */

public class EventCodeDialog extends BaseDialog<EventCodeDialog> {


    private Context mContext;
    private ImageView ivOut;
    private ImageView ivCode;
    private Button btnCodeDownload;

    public EventCodeDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    public View onCreateView() {
        widthScale(0.75f);
        View inflate = View.inflate(context, R.layout.dialog_event_code, null);
        ivCode = (ImageView) inflate.findViewById(R.id.dialog_event_code_iv_code);
        ivOut = (ImageView) inflate.findViewById(R.id.dialog_event_code_iv_out);
        btnCodeDownload = (Button) inflate.findViewById(R.id.dialog_event_code_btn_code_download);




        return inflate;
    }

    @Override
    public void setUiBeforShow() {
        ivOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        btnCodeDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SaveImageToSysAlbum();  //保存到相册
                dismiss();
            }
        });
    }


    //生成二维码
    public void encode(String contents) {
        Bitmap mBitmap = CodeUtils.createImage(contents, 400, 400, null);
        ivCode.setImageBitmap(mBitmap);
    }

    public void setCode(String img){
        Glide.with(getContext())
                .load(img)
                .dontAnimate()
                .placeholder(R.mipmap.ic_wait_logo)
                .error(R.mipmap.ic_error)
                .into(ivCode);
    }


    private void SaveImageToSysAlbum() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Bitmap bitmap = convertViewToBitmap(ivCode);
            if (bitmap != null) {
                try {
                    ContentResolver cr = mContext.getContentResolver();
                    String url = MediaStore.Images.Media.insertImage(cr, bitmap,
                            String.valueOf(System.currentTimeMillis()), "");
                    Toast.makeText(mContext, "保存成功", Toast.LENGTH_SHORT).show();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                Toast.makeText(mContext, "保存失败", Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(mContext, "没有存储权限，保存失败", Toast.LENGTH_SHORT).show();
        }
    }

    public static Bitmap convertViewToBitmap(View view)
    {
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }





}
